package com.work.biz.controller;

import java.io.IOException;
import java.util.List;

import com.work.biz.domain.BizNoticeDetail;
import com.work.biz.service.IBizNoticeDetailService;
import com.work.mp.service.impl.MpTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.work.common.annotation.Log;
import com.work.common.core.controller.BaseController;
import com.work.common.core.domain.AjaxResult;
import com.work.common.enums.BusinessType;
import com.work.biz.domain.BizNotice;
import com.work.biz.service.IBizNoticeService;
import com.work.common.utils.poi.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 通知信息Controller
 *
 * @author lee
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/api/biz/notice")
@Api(value = "通知api", tags = {"新增通知、修改通知，通知查询接口"})
public class BizNoticeController extends BaseController {
    @Autowired
    private IBizNoticeService bizNoticeService;
    @Autowired
    private IBizNoticeDetailService noticeDetailService;
    @Autowired
    private MpTokenService tokenService;

    /**
     * 查询未读通知信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询我发布的通知", httpMethod = "GET", notes = "成功：0，验证失败：500")
    public AjaxResult list(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk
    ) {
        BizNotice bizNotice = new BizNotice();
        bizNotice.setCreatorId(tokenService.getUserId(tk));
        List<BizNotice> list = bizNoticeService.selectBizNoticeList(bizNotice);
        return AjaxResult.success(list);
    }

    /**
     * 查询未读通知信息列表
     */
    @PostMapping("/readList")
    @ApiOperation(value = "查询学生通知", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult readList(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "通知信息检索条件", required = true)
            @RequestBody
                    BizNoticeDetail noticeDetail
    ) {

        noticeDetail.setUserId(tokenService.getUserId(tk));
        List<BizNoticeDetail> list = noticeDetailService.selectBizNoticeDetailList(noticeDetail);
        return AjaxResult.success(list);
    }

    /**
     * 导出通知信息列表
     */
    @Log(title = "通知信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizNotice bizNotice) {
        List<BizNotice> list = bizNoticeService.selectBizNoticeList(bizNotice);
        ExcelUtil<BizNotice> util = new ExcelUtil<BizNotice>(BizNotice.class);
        return util.exportExcel(list, "通知信息数据");
    }

    /**
     * 获取通知信息详细信息
     */
    @GetMapping(value = "/{noticeId}")
    @ApiOperation(value = "教师查看通知详情", httpMethod = "GET", notes = "成功：0，验证失败：500")
    public AjaxResult getInfo(
            @ApiParam(value = "通知ID", required = true)
            @PathVariable("noticeId") Long noticeId) {
        return AjaxResult.success(bizNoticeService.selectBizNoticeById(noticeId));
    }

    /**
     * 提交通知阅读（修改通知信息详情）
     */
    @Log(title = "提交通知阅读", businessType = BusinessType.UPDATE)
    @PutMapping("/read")
    @ApiOperation(value = "提交通知阅读", httpMethod = "PUT", notes = "成功：0，验证失败：500")
    public AjaxResult read(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "通知信息")
            @RequestBody BizNoticeDetail bizNoticeDetail
    ) {
        return toAjax(noticeDetailService.updateBizNoticeDetail(bizNoticeDetail));
    }

    /**
     * 发布文字通知
     */
    @Log(title = "通知信息", businessType = BusinessType.INSERT)
    @PostMapping("/addText")
    @ApiOperation(value = "发布文字通知", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult addText(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk") String tk,
            @ApiParam(value = "通知信息", required = true)
            @RequestBody BizNotice bizNotice
    ) {
        bizNotice.setCreatorId(tokenService.getUserId(tk));
        return toAjax(bizNoticeService.insertBizNotice(bizNotice));
    }

    /**
     * 发布文档通知
     */
    @Log(title = "通知信息", businessType = BusinessType.INSERT)
    @PostMapping("/addDoc")
    @ApiOperation(value = "发布文档通知", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult addDoc(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "通知信息")
                    BizNotice bizNotice,
            @ApiParam(value = "通知文档")
                    MultipartFile file
    ) throws Exception {
        if ("2".equals(bizNotice.getNoticeType())) {
            ExcelUtil<BizNoticeDetail> util = new ExcelUtil<>(BizNoticeDetail.class);
            List<BizNoticeDetail> details = util.importExcel(file.getInputStream());
            bizNotice.setDetails(details);
        }
        bizNotice.setCreatorId(tokenService.getUserId(tk));
        return toAjax(bizNoticeService.insertBizNotice(bizNotice));
    }

    /**
     * 修改通知信息
     */
    @Log(title = "通知信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "修改通知信息", httpMethod = "PUT", notes = "成功：0，验证失败：500")
    public AjaxResult edit(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "通知信息")
            @RequestBody BizNotice bizNotice
    ) {
        return toAjax(bizNoticeService.updateBizNotice(bizNotice));
    }

    /**
     * 删除通知信息
     */
    @Log(title = "通知信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return toAjax(bizNoticeService.deleteBizNoticeByIds(noticeIds));
    }
}
